package cn.com.oceansoft.business.os.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.os.entity.*;
import cn.com.oceansoft.business.os.service.ILuckyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.*;

import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by zhangxl on 2016/12/20.
 */

@Controller
@RequestMapping("lucky")
public class LuckyController extends BaseController {

    @Resource
    private ILuckyService luckyService;

    @RequestMapping(value = "index", method = GET)
    public String openRoleIndex(Query query, Model model) {
        model.addAttribute("query", query);
        model.addAttribute("pager", luckyService.findPageByCondition(query));
        log(String.format("查看奖项配置%d页", query.getPageNum()));
        return "os/lucky/lucky";
    }

    @RequestMapping(value = "list", method = POST)
    public String loadRoleByPage(Query query, Model model) {
        model.addAttribute("query", query);
        model.addAttribute("pager", luckyService.findPageByCondition(query));
        log(String.format("查看奖项配置列表第%d页", query.getPageNum()));
        return "os/lucky/lucky";
    }

    @RequestMapping(value = "new", method = GET)
    public String openAddLucky() {
        return "os/lucky/lucky-new";
    }

    @ResponseBody
    @RequestMapping(value = "new", method = POST)
    public Result addLucky(Lucky lucky) {
        return luckyService.save(lucky);
    }

    @ResponseBody
    @RequestMapping(value = "edit", method = POST)
    public Result editRole(Lucky lucky) {
        return luckyService.update(lucky);
    }

    @RequestMapping(value = "view/{lucky_id}", method = GET)
    public String viewRole(@PathVariable("lucky_id") int luckyId, Model model) {
        model.addAttribute("lucky", luckyService.findById(luckyId));
        return "os/lucky/lucky-edit";
    }

    @ResponseBody
    @RequestMapping(value = "{lucky_id}", method = DELETE)
    public Result deleteRole(@PathVariable("lucky_id") int luckyId) {
        luckyService.deleteLuckyById(luckyId);
        return new Result();
    }

    @RequestMapping(value = "tree/{lucky_id}", method = GET)
    public String viewLuckyPerson(@PathVariable("lucky_id") int luckyId, Model model) {
        List<User> list = luckyService.findLuckyPersonByLuckyId(luckyId);
        Map<Integer, DeptTree> treeMap = new LinkedHashMap<Integer, DeptTree>();
        List<Dept> deptlist = luckyService.findDept();
        Lucky lucky = luckyService.findLuckyById(luckyId);
        for (Dept dept : deptlist) {
            DeptTree group = new DeptTree();
            group.setFlag(dept.getFlag());
            group.setText(dept.getName());
            List<User> userlist = new ArrayList<User>();
            group.setLeaf(userlist);
            treeMap.put(dept.getCode(), group);
        }
        for (User res : list) {
            System.out.println(Integer.parseInt(res.getDept()));
            System.out.println(treeMap.get(Integer.parseInt(res.getDept())));
            treeMap.get(Integer.parseInt(res.getDept())).getLeaf().add(res);
        }

        model.addAttribute("luckyName", lucky.getName());
        model.addAttribute("luckyId", luckyId);
        model.addAttribute("tree", CollectionUtils.arrayToList(treeMap.values().toArray()));

        return "os/lucky/lucky-p";
    }

    @ResponseBody
    @RequestMapping(value = "bind/{lucky_id}", method = POST)
    public Result bindLuckyPerson(@PathVariable("lucky_id") int luckyId, @RequestParam("ids") int[] ids) {
        luckyService.updateLuckyPersonBatch(luckyId, ids);
        return new Result();
    }

    @RequestMapping(value = "check4/{lucky_id}", method = GET)
    public String check4Users(@PathVariable("lucky_id") int luckyId, Model model) {
        List<User> fUser = luckyService.check4User(luckyId);
        Map<Integer, DeptTree> treeMap = new LinkedHashMap<Integer, DeptTree>();
        List<Dept> deptlist = luckyService.findDept();
        Lucky lucky = luckyService.findLuckyById(luckyId);
        for (Dept dept : deptlist) {
            DeptTree group = new DeptTree();
            group.setFlag(dept.getFlag());
            group.setText(dept.getName());
            List<User> userlist = new ArrayList<User>();
            group.setLeaf(userlist);
            treeMap.put(dept.getCode(), group);
        }
        for (User res : fUser) {

            treeMap.get(Integer.parseInt(res.getDept())).getLeaf().add(res);
        }
        model.addAttribute("luckyName", lucky.getName());
        model.addAttribute("luckyId", luckyId);
        model.addAttribute("tree", CollectionUtils.arrayToList(treeMap.values().toArray()));

        return "os/lucky/lucky-p";
    }

    @RequestMapping(value = "result/{lucky_id}", method = GET)
    public String resultLuckyBoy(@PathVariable("lucky_id") int luckyId, Model model) {
        model.addAttribute("luckyBoy", luckyService.findLuckyBoyById(luckyId));
        return "os/lucky/luckyBoy";
    }

}
